using B.S.Data.MES.API.Applications.BaseData.Command.BOMCommand;
using B.S.Domain.BasicData.MaterialManagement.BOM;
using B.S.Domain.BasicData.MaterialManagement.Material_type;
using B.S.Domain.BasicData.MaterialManagement.MaterialGroups;
using B.S.Domain.BasicData.MaterialManagement.Product_material;
using B.S.Domain.Dto.WuLiaoDto;
using B.S.Domain.Process.ProcessrouteName;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.BOMCommandHandlers
{
    public class QueryBOMWithRelatedTablesCommandHandler : IRequestHandler<QueryBOMWithRelatedTablesCommand, APIRequst<List<BOMDTO>>>
    {
        private readonly IBaseRepository<BOM> _bomRepository;
        private readonly IBaseRepository<Material_type> _materialTypeRepository;
        private readonly IBaseRepository<Material_group> gropuRepository;
        private readonly IBaseRepository<Product_material> proDuctRepository;
        private readonly IBaseRepository<Processroute> ProRepository;

        public QueryBOMWithRelatedTablesCommandHandler(
            IBaseRepository<BOM> bomRepository,
            IBaseRepository<Material_type> materialTypeRepository,
            IBaseRepository<Material_group> gropuRepository,
            IBaseRepository<Processroute> proRepository,
            IBaseRepository<Product_material> proDuctRepository)
        {
            _bomRepository = bomRepository;
            _materialTypeRepository = materialTypeRepository;
            this.gropuRepository = gropuRepository;
            ProRepository = proRepository;
            this.proDuctRepository = proDuctRepository;
        }

        public Task<APIRequst<List<BOMDTO>>> Handle(QueryBOMWithRelatedTablesCommand request, CancellationToken cancellationToken)
        {
            // 使用LINQ实现多表连接查询，以BOM表为主表
            var query = from a in _bomRepository.GetAll()
                        join b in gropuRepository.GetAll() on a.Materialcode equals b.Materialcode
                        join c in _materialTypeRepository.GetAll() on b.Materialtypeid equals c.Materialtypeid
                        join d in proDuctRepository.GetAll() on c.Materialtypeid equals d.Grouptype
                        join e in ProRepository.GetAll() on d.processroute equals e.Id
                        where !a.isDel // 过滤掉已删除的BOM记录
                        select new BOMDTO
                        {
                            BomId = a.BomId,
                            Materialcode = a.Materialcode,
                            Bname = a.Bname,
                            version = a.version,
                            describe = a.describe,
                            MaterialTypeId = c.Materialtypeid,
                            MaterialTypeName = c.MaterialtypeName,
                            MaterialGroupId = b.MaterId,
                            MaterialGroupName = b.Materialname,
                            ProductId = d.Productid,
                            ProductName = d.ProductName,
                            Specifications = d.specifications,
                            Unit = d.unit,
                            ProductDescribe = d.describe,

                            ProcessrouteName = e.ProcessrouteName,
                            ProcessrouteType = e.ProcessrouteType,
                        };

            return Task.FromResult(new APIRequst<List<BOMDTO>>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data = query.ToList()
            });
        }
    }
}